<?php

class tStructure_Folder extends tStandard_TreeItem 
{

	function view(cView $view) 
	{
		$this->complete($view);
		
		$this->order = $this->getCustom('order');
		
		if ($view->mode!="backend")
		if (is_numeric($id = $this->getCustom('default_id')) && $id!=0) 
		{		
			
			$n = $this->cms->tree->getNodeById($id, $view);
			
			// aby se jevil jako ten, kterym je default_id
			$this->setData($n->data);
			return $n->view($view);
		} 
		
		return parent::view($view);
	}
	
	function beforeView() 
	{
		parent::beforeView();
		$browser = $this->definition->xpath('//browser');
		$browser[0]['where']=$this->getCustom('where');
		$browser[0]['paging']=$this->getCustom('paging');
		$browser[0]['rel']=$this->getCustom('relation');
	}
	
	
	function setSXML(SimpleXMLElement $inSXML, cView $view, SimpleXMLElement $outSXML)
	{
		parent::setSXML($inSXML, $view, $outSXML);
		if ($view->doctype == 'raw') return; 
		
		//	pokud bechceme vidět výpis v adminovi, opustíme vykonávání
		if ($view->action!='show' || $view->mode!='backend') return ;
		
		$outSXML->structure_folder->addChild("toolbar",$this->getToolbar());

		/*	ONEUP
		} else {
			$s.="
			<tr>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<td><a href=\"".$view->getUrl(array("id"=>$this->parent_id))."\" class=\"oneup\" >[výš nelze již]</a></td>
				<td>".$node->visibility."</td>
				<td>".$node->created_at."</td>
				<td>&nbsp;</td>
			</tr>";	
		}
		*/
		
		return ; 
	}
	
	function getToolbar()
	{
		$allowedTypes = explode(",",$this->getCustom('types')); 
		$s.="<div class=\"toolbar\" >přidat ";
		$info = $this->cms->file->listTypes();
		foreach ($info as $group=>$types) {
			foreach ($types as $type=>$name) {
				if (in_array($type,$allowedTypes)) 
				{
					$s.="\n<form method=\"post\">";
					$s.="\n<input type=\"hidden\" name=\"group\" value=\"$group\" />";
					$s.="\n<input type=\"hidden\" name=\"type\" value=\"$type\" />";
					$s.="\n<input type=\"hidden\" name=\"action\" value=\"insert\" />"; //pro view
					$s.="\n<input type=\"hidden\" name=\"parent_id\" value=\"".$this->id."\" />";
					$s.="\n<input type=\"hidden\" name=\"id\" value=\"new\" />";
					$s.="\n<input type=\"submit\" value=\"$name\" /> ";
					$s.="</form>";
				}
			}
		}
		

		if (is_array($this->cms->user->clipboard["cut"])) {
			$s.=" ".count($this->cms->user->clipboard["cut"])." položek, ";
			$s.="
				<form method=\"post\">
					<input type=\"hidden\" name=\"aname\" value=\"paste\" />
					<input type=\"hidden\" name=\"type\" value=\"".$this->type."\" />
					<input type=\"hidden\" name=\"group\" value=\"".$this->group."\" />
					<input type=\"hidden\" name=\"id\" value=\"".$this->id."\" />
					<input type=\"submit\" name=\"_action[massAction]\" value=\"vložit sem\"/>
					, nebo
					<input type=\"submit\" name=\"_action[emptyHand]\" value=\"vrátit\"/>
				</form>
				"; 
		}
		
		$s.="</div><form method=\"post\"><div class=\"massaction\">";
		
		
		$actions = array("cut","delete","publish","unpublish","makePrivate");
		$sel.="<option value=\"\" >vybrané..</option>";

		foreach ($actions as $a) {
			$sel.="<option value=\"$a\">$a</option>";
		}
		$s.="
				<input type=\"hidden\" name=\"type\" value=\"".$this->type."\" />
				<input type=\"hidden\" name=\"group\" value=\"".$this->group."\" />
				<input type=\"hidden\" name=\"id\" value=\"".$this->id."\" />
				<select name=\"aname\" class=\"autopop\" >$sel</select>
				<input type=\"submit\" name=\"_action[massAction]\" value=\"go!\" />
			";
		
		return $s.'</div>';		
	}
	
	
	function handleEmptyHand($post) 
	{
		$this->cms->user->setClipBoard("cut",null);
	}
	
	function handleMassAction($post, cView $view) 
	{
		//print_r($post);
		$action = $post["aname"];
		if (strToLower($action)!="post" && strToLower($action)!="massaction")
		if ($action=="cut") 
		{
			foreach ($post["nodes"] as $id=>$is_on) $ids[]=$id;
			$this->cms->user->setClipBoard("cut",$ids); //hodit do clipboardu			
		} 
		else if ($action=="paste") 
		{
			foreach ($this->cms->user->clipboard["cut"] as $id) {
				$n = $this->cms->tree->getNodeById($id, $view);
				$n->complete();
				$n->moveTo($post["id"]);
			}
			$this->cms->user->setClipBoard("cut",null);
							
		} else {
			try { 		
				$this->cms->tree->query("START TRANSACTION");
				foreach ($post["nodes"] as $id=>$whaeva) 
				{
					$n = $this->cms->tree->getNodeById($id, $view);
					if ($action=="delete") $n->delete(false, $view);
					if ($action=="publish") $n->publish(false, $view);
					if ($action=="unpublish") $n->unpublish(false, $view);
					if ($action=="makePrivate") $n->makePrivate(false, $view);
				}
				$this->cms->tree->query("COMMIT");
			} catch (Exception $e) {
				$this->cms->tree->query("ROLLBACK");
				throw $e;				
			}
		}
	}
	
}


?>